package com.example.demo.services.impl;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.dao.ArticleMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Article;
import com.example.demo.model.Board;
import com.example.demo.model.User;
import com.example.demo.services.IArticleService;
import com.example.demo.services.IBoardService;
import com.example.demo.services.IUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ArticleServiceImpl implements IArticleService {
    @Resource
    private IUserService userService;

    @Resource
    private IBoardService boardService;

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public void create(Article article) {
        //非空校验
        if(article == null
                || article.getBoardId() == null
                || article.getUserId() == null
                || article.getTitle() == null
                || article.getContent() == null){
            //日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        article.setVisitCount(0);
        article.setReplyCount(0);
        article.setLikeCount(0);
        article.setState((byte) 0);
        article.setDeleteState((byte) 0);
        Date date=new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);
        //写入数据库
        int articleRow= articleMapper.insertSelective(article);
        //判断是否写入成功
        if(articleRow!= 1){
            //日志
            log.info(ResultCode.FAILED_CREATE.getMessage());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_CREATE));
        }
        //获取y用户信息
        User user=userService.selectById(article.getUserId());
        if(user==null){
            //日志
            log.info(ResultCode.FAILED_CREATE.getMessage());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //更新用户文章数量
        userService.addArticleCountById(user.getId());

        //获取板块
        Board board=boardService.selectById(article.getBoardId());
        if(board==null){
            //日志
            log.info(ResultCode.FAILED_CREATE.getMessage());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //更新板块文章数量
        boardService.addOneArticleCount(board.getId());
    }

    @Override
    public List<Article> selectAll() {
        //调用dao查询
        List<Article> articles=articleMapper.selectAll();
        //返回结果
        return articles;
    }

    @Override
    public List<Article> selectByBoardId(Long articleId) {
        if(articleId==null || articleId<=0){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_USER_NOT_EXISTS));
        }

        Board board=boardService.selectById(articleId);
        if(board==null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        //调用dao查询
        List <Article> articles=articleMapper.selectByBoardId(articleId);

        return articles;
    }




    @Override
    public Article selectDetailById(Long id) {
        if(id==null || id<0){
            //日志
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //调用dao查询
        Article article=articleMapper.selectDetailById(id);
        if(article==null){
            //日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }

        //更新帖子的访问数
        Article updateArticle=new Article();
        updateArticle.setId(id);
        updateArticle.setVisitCount(article.getVisitCount()+1);

        //保存到数据库
        int row =articleMapper.updateByPrimaryKeySelective(updateArticle);
        //判断是否更新成功
        if(row!=1){
            //日志
            log.warn(ResultCode.FAILED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED));
        }

        article.setVisitCount(article.getVisitCount()+1);
        return article;
    }

    @Override
    public Article selectById(Long id) {
        if(id==null || id<0){
            //   日志
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            //   抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_NOT_EXISTS));
        }
        //调用dao查询
        Article article=articleMapper.selectByPrimaryKey(id);

        return article;
    }

    @Override
    public void modify(Long id, String title, String content) {
        if(id==null || id<0
                || title==null
                || content==null){
            //   日志
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            //   抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_NOT_EXISTS));
        }
        Article updateArticle=new Article();
        updateArticle.setId(id);
        updateArticle.setTitle(title);
        updateArticle.setContent(content);
        updateArticle.setUpdateTime(new Date());


        int row=articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row!=1){
            //日志
            log.warn(ResultCode.FAILED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED));
        }
    }

    @Override
    public void thumbsUpById(Long id) {
        if(id==null || id<0){
            //日志
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //调用dao查询
        Article article=articleMapper.selectDetailById(id);
        //判断是否存在
        if(article==null){
            //日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //帖子状态判断
        if(article.getState()==1 ){
            //日志
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_BOARD_BANNED));
        }
        //更新 thumbsUpCount
        Article updateArticle=new Article();
        updateArticle.setId(id);
        updateArticle.setLikeCount(article.getLikeCount()+1);

        int row=articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row!=1){
            //日志
            log.warn(ResultCode.FAILED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED));
        }



    }

    @Override
    public void deleteById(Long id) {
        if(id==null || id<0){
            //日志
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //调用dao查询
        Article article=articleMapper.selectDetailById(id);
        //判断是否存在
        if(article==null || article.getDeleteState()==1){
            //日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }

        //新建
        Article updateArticle=new Article();
        updateArticle.setId(article.getId());
        updateArticle.setDeleteState((byte) 1);

        //调用dao更新
        int row=articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row!=1){
            //日志
            log.warn(ResultCode.FAILED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED));
        }
        userService.subOneArticleCountById(article.getUserId());
        boardService.subOneArticleCountById(article.getBoardId());

        log.info("删除帖子成功, article id = " + article.getId() + ", user id = " + article.getUserId() + ".");

    }

    @Override
    public void addOneReplyCountId(Long id) {
        if(id==null || id<0){
            //日志
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_USER_NOT_EXISTS));
        }
        Article article=articleMapper.selectByPrimaryKey(id);

        if(article==null){
            //日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if(article.getState()==1){
            //日志
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED_ARTICLE_BANNED));
        }

        //构造更新对象
        Article updateArticle=new Article();
        updateArticle.setId(id);
        updateArticle.setReplyCount(article.getReplyCount()+1);
        updateArticle.setUpdateTime(new Date());

        //调用dao更新
        int row =articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row!=1){
            //日志
            log.warn(ResultCode.FAILED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed( ResultCode.FAILED));
        }


    }


}
